﻿using System.Security.Cryptography.X509Certificates;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Sundstrom.Mvvm.Tests.ViewModels
{
    [TestClass]
    public class ExtendedViewModelBaseTest
    {
        [TestMethod]
        public void ValidatePropertyWithAnnotation_Valid()
        {
            var validatedViewModel = new ValidatedViewModel();
            validatedViewModel.Name = "Robert";

            Assert.IsTrue(validatedViewModel.IsValid);
        }

        [TestMethod]
        public void ValidatePropertyWithAnnotation_Invalid()
        {
            var validatedViewModel = new ValidatedViewModel();
            validatedViewModel.Name = "";

            Assert.IsFalse(validatedViewModel.IsValid);
        }

        [TestMethod]
        public void ValidatePropertyWithValidationMethod_Valid()
        {
            var validatedViewModel = new ValidatedViewModel();
            validatedViewModel.Age = 24;

            validatedViewModel.ValidateProperty((vm) => vm.Age);

            Assert.IsTrue(validatedViewModel.IsValid);
        }

        [TestMethod]
        public void ValidatePropertyWithValidationMethod_Invalid()
        {
            var validatedViewModel = new ValidatedViewModel();
            validatedViewModel.Age = 0;

            validatedViewModel.ValidateProperty((vm) => vm.Age);

            Assert.IsFalse(validatedViewModel.IsValid);
        }


        [TestMethod]
        public void ValidateAll_Successful()
        {
            var validatedViewModel = new ValidatedViewModel();

            validatedViewModel.Name = "";
            validatedViewModel.Age = 0;

            validatedViewModel.Validate();

            Assert.IsFalse(validatedViewModel.IsValid);
            Assert.IsTrue(validatedViewModel.HasErrors);

            validatedViewModel.Name = "Robert";
            validatedViewModel.Age = 24;

            validatedViewModel.Validate();

            Assert.IsTrue(validatedViewModel.IsValid);
            Assert.IsFalse(validatedViewModel.HasErrors);

        }

        [TestMethod]
        public void ValidateAll_ClearErrors_ErrorsCleared_HasNoErrors()
        {
            var validatedViewModel = new ValidatedViewModel();

            validatedViewModel.Name = "";
            validatedViewModel.Age = 0;

            validatedViewModel.Validate();

            validatedViewModel.ClearErrors();
            
            Assert.IsFalse(validatedViewModel.HasErrors);
            Assert.IsTrue(validatedViewModel.IsValid);
        }
    }
}